<?php

class SellerAuth {

	private $_model_seller;

	/*
	授权信息
	*/
	public $top_session = NULL;
	public $w2time = 0;
	public $last_name = NULL;

	/*
    * ------------------------------------------------------
    *  构建函数
    * ------------------------------------------------------
    */
	public function __construct()
	{
		$TATA =& get_instance();
		$this->_model_seller = $TATA->load->load_class('seller_mdl');

		if(DEV_MODE == 'local'){
			 /*$_SESSION['id'] = '1';
			 $_SESSION['tb_uid'] = '1862941194';
			 $_SESSION['tb_nick'] = '成都盛夏科技';
			 $_SESSION['top_session'] = '6102813b3f70d4fc963a418e5dbf0ff57bdea8fdf5112081862941194';
			 $_SESSION['last_name'] = '成都盛夏科技';
			 $_SESSION['timeout'] = '18600';*/

			
			$_SESSION['id'] = '2';
			$_SESSION['tb_uid'] = '3170729146';
			$_SESSION['tb_nick'] = 'MAC魅可官方旗舰店';
			$_SESSION['top_session'] = '6101c01ad17410a22f10795190678f67ef0fa20a7e083343170729146';
			$_SESSION['last_name'] = 'MAC魅可官方旗舰店';
			$_SESSION['timeout'] = '18600';

			$_mdl = Loader::mdl('seller_mdl');
			$_mdl->add_seller(
				$_SESSION['tb_uid'],
				$_SESSION['tb_nick'],
				$_SESSION['top_session'],
				$_SESSION['last_name']
			);

			//dump('%E6%88%90%E9%83%BD%E7%9B%9B%E5%A4%8F%E7%A7%91%E6%8A%80');
			//dump(urldecode('%E6%88%90%E9%83%BD%E7%9B%9B%E5%A4%8F%E7%A7%91%E6%8A%80'));
		}else {
            $_SESSION['id'] = '2';
            $_SESSION['tb_uid'] = '3170729146';
            $_SESSION['tb_nick'] = 'MAC魅可官方旗舰店';
            $_SESSION['top_session'] = '6101c01ad17410a22f10795190678f67ef0fa20a7e083343170729146';
            $_SESSION['last_name'] = 'MAC魅可官方旗舰店';
            $_SESSION['timeout'] = '18600';

            $_mdl = Loader::mdl('seller_mdl');
            $_mdl->add_seller(
                $_SESSION['tb_uid'],
                $_SESSION['tb_nick'],
                $_SESSION['top_session'],
                $_SESSION['last_name']
            );

			/*if ($_GET['code']) {
				$this->auth($_GET['code']);
			} else {
				return false;
			}*/
		}
	}

	public function auth($code)
	{
		//https://oauth.taobao.com/authorize?response_type=code&client_id=23272568&redirect_uri=https://smart.ews.m.jaeapp.com
		$url = 'https://oauth.taobao.com/token';
		$postfields= array('grant_type'=>'authorization_code',
			'client_id'=>appKey,
			'client_secret'=>appSecret,
			'code'=>$code,
			'redirect_uri'=>'https://smart.ews.m.jaeapp.com');
		$post_data = '';

		foreach($postfields as $key=>$value){
			$post_data .="$key=".urlencode($value)."&";}
		$ch = curl_init();

		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
		//指定post数据

		curl_setopt($ch, CURLOPT_POST, true);
		//添加变量

		curl_setopt($ch, CURLOPT_POSTFIELDS, substr($post_data,0,-1));
		$output = curl_exec($ch);
		$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

		//echo $httpStatusCode;
		curl_close($ch);
		//var_dump($output);

		$output = json_decode($output, true);
		//dump(urldecode_utf8($output['taobao_user_nick']));
		//dump($output);

		if($output['taobao_user_id'] > 0) {
			$tb_nick = urldecode($output['taobao_user_nick']);
			//dump($output['taobao_user_nick']);
			//dump($tb_nick);
			$tb_uid = $output['taobao_user_id'];
			$top_session = $output['access_token'];
			$last_name = $output['sub_taobao_user_nick'] ?  urldecode($output['sub_taobao_user_nick']): $tb_nick;
			$w2time =  $output['w2_expires_in'];

			//echo $_SESSION['sessionKey'].'---'.$_SESSION['nick'].'---'.$_SESSION['uid'];
			// 保存用户信息到数据库
			//$appLog->info(':: save to db t_seller >>>>>> ');
			$seller = $this->_model_seller->add_seller(
				$tb_uid,
				$tb_nick,
				$top_session,
				$last_name
			);

			//保存用户信息到SESSION缓存
			$_SESSION['tb_nick'] = $tb_nick;
			$_SESSION['tb_uid'] = $tb_uid;
			$_SESSION['top_session'] = $top_session;
			$_SESSION['last_name'] = $last_name;
			$_SESSION['timeout'] = $w2time;


			//$appLog->info(':: save to db t_seller <<<<<< ');
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
}